<?php if ( ! defined('ABSPATH')) exit('restricted access');

function texttoslug($string)
{
	return trim(preg_replace("#([^a-z0-9])#i","_",strtolower($string)));
}

function slugtotext($string)
{
	return ucwords(trim(preg_replace("#([^a-z0-9])#i"," ",strtolower($string))));
}

function fw_get_pages($args = '')
{
	$pages = get_pages($args);
	foreach($pages as $page)
	{
		$pages_arr[$page->ID] = $page->post_title;
	}
	
	return $pages_arr;
}

function multi_categories($settings = array())
{
	$categories = fw_get_categories();
	$size = (count($categories) < 10) ? count($categories) * 20 : 220;
	return form_multiselect('categories[selected][]', $categories, $settings['categories'][0], 'style="height:'.$size.'px;"' );	
}

/**
 * current_user_role
 *
 * Get the current wordpress user role
 *
 * @access	public
 * @example	current_user_role();
 * @param	void
 * @return	string
 */
	 
function current_user_role()
{
	return key($GLOBALS['current_user']->caps);
}

function search_node($array, $node)
{
	foreach($array as $k=>$v)
	{
		if( ! is_array($v)) return false;
		if($k == $node)
		{
			return $v;
		}
		elseif(is_array($v))
		{
			$v = search_node($v, $node);
			if($v) return $v;
		}
	}
	return false;
}

function last_nodes($array = array(), $options = array())
{
	if ( ! $options) return array();
	
	if ( ! is_array($array))
	{
		if( ! isset($options[$array])) return false;
		$array = $options[$array];
	}
	
	$settings = array();
	foreach($array as $k=>$v)
	{
		if($k == 'DYNAMIC')
		{
			$settings['DYNAMIC'] = array();
			foreach($v as $dk=>$dv)
			{
				$settings['DYNAMIC'] = array_merge((array) $settings['DYNAMIC'], (array)setting_node($dv, $dk));
			}
		}else $settings = array_merge($settings,(array) setting_node($v, $k));
	}
	
	return $settings;
}

function setting_node($array = array(), $key)
{
	if ( ! is_array($array)) return array();
	elseif(isset($array['label'])) return array($key=>$array);
	
	$settings = array();
	foreach($array as $k=>$v)
	{	
		if(is_array($v))
		{
			$match = setting_node($v, $k);
			if($match)
			{
				$settings = array_merge($settings, $match);
			}
		}
	}

	return $settings;
}

/** A function to fetch the categories from wordpress */
function fw_get_categories($arg = false)
{
	global $wp_taxonomies;
	if( ! empty($arg['taxonomy']) && ! isset($wp_taxonomies[$arg['taxonomy']]))
	{
		register_taxonomy( $arg['taxonomy'], THEME_PREFIX.$arg['taxonomy']);
	}

	$categories = get_categories($arg);

	foreach($categories as $category)
	{
		$cats[$category->term_id] = $category->name;
	}
	return $cats;
}

if ( ! function_exists('word_limiter'))
{
	function word_limiter($str, $limit = 100, $end_char = '&#8230;')
	{
		if (trim($str) == '') return $str;
		preg_match('/^\s*+(?:\S++\s*+){1,'.(int) $limit.'}/', $str, $matches);
		if (strlen($str) == strlen($matches[0]))
		{
			$end_char = '';
		}
		
		return rtrim($matches[0]).$end_char;
	}
}

if ( ! function_exists('character_limiter'))
{
	function character_limiter($str, $n = 500, $end_char = '&#8230;', $allowed_tags = false)
	{
		if($allowed_tags) $str = strip_tags($str, $allowed_tags);
		if (strlen($str) < $n)	return $str;
		$str = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str));

		if (strlen($str) <= $n) return $str;

		$out = "";
		foreach (explode(' ', trim($str)) as $val)
		{
			$out .= $val.' ';
			
			if (strlen($out) >= $n)
			{
				$out = trim($out);
				return (strlen($out) == strlen($str)) ? $out : $out.$end_char;
			}		
		}
	}
}

function array_to_string($array = array(), $options = array())
{
	$default = array('wrap_s'=>'="','wrap_e'=>'" ');
	if( ! is_array($options)) parse_str($options, $options);
	
	$options = array_merge($default, $options);
	
	$return = '';
	foreach($array as $k=>$v)
	{
		$return .= $k.$options['wrap_s'].$v.$options['wrap_e'];
	}
	
	return $return;
}